/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.uniandes.lab1.muebles.anotaciones;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

/**
 *
 * @author Oscar Ballesteros
 */
@SupportedAnnotationTypes({"co.edu.uniandes.lab1.muebles.anotaciones.mueble"})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class ProcesadorAnotaciones extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        System.out.println(roundEnv.getElementsAnnotatedWith(mueble.class));
        List<TypeElement> clasesInternas = new ArrayList<TypeElement>();
        List<TypeElement> clasesExternas = new ArrayList<TypeElement>();
        if(roundEnv.getElementsAnnotatedWith(mueble.class).size() == 0){
            return true;
        }
        for (Element e : roundEnv.getElementsAnnotatedWith(mueble.class)) {
            // Debe ser una clase
            Element classElement = e;
            System.out.println(classElement.getSimpleName());
            // Obtener el nombre de la clase incluuido el paquete al que pertence
            TypeElement tipoClase = (TypeElement) classElement;
            String className = ((TypeElement) classElement).getQualifiedName().toString();
            mueble a = tipoClase.getAnnotation(mueble.class);
            System.out.println(a.tipoMueble() + " "+TipoMueble.INTERIOR);
            System.out.println(className);
            
            if(a.tipoMueble().equals(TipoMueble.EXTERIOR)){
                clasesExternas.add(tipoClase);
            }else if(a.tipoMueble().equals(TipoMueble.INTERIOR)){
                clasesInternas.add(tipoClase);
            }

        }
        File file = new File("C:/Catalogo.txt");
        System.out.println("Clases Externas "+clasesExternas);
        System.out.println("Clases Interna "+clasesInternas);
        try {
            System.out.println(file.createNewFile()) ;
            int i = 0;
            PrintWriter pw = new PrintWriter(file);
            pw.println("Interior");
            for(TypeElement tei : clasesInternas){
                pw.println(i+"\t"+tei.getSimpleName());
                i++;
            }
            pw.println("Exterior");
            for(TypeElement tex : clasesExternas){
                pw.println(i+"\t"+tex.getSimpleName());
                i++;
            }
            pw.flush();
            pw.close();
        } catch (IOException ex) {
            Logger.getLogger(ProcesadorAnotaciones.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}
